package com.zh.singletonmode.services.lazy;

public class InnerClassLazySingletonCompulsory {
    //调用InnerClassLazySingletonCompulsory时，默认会先初始化内部类，不调用时，内部类不加载
    private InnerClassLazySingletonCompulsory(){
        //判断是否已经创建实例
        if (LazyHolder.innerClassLazySingletonCompulsory != null){
            throw new RuntimeException("不允许创建多个实例");
        }
    }

    public static final InnerClassLazySingletonCompulsory getInstance(){
        //方法执行前内部类已经加载，并初始化了对象
        return LazyHolder.innerClassLazySingletonCompulsory;
    }

    //默认不加载
    private static class LazyHolder{
        private static final InnerClassLazySingletonCompulsory innerClassLazySingletonCompulsory = new InnerClassLazySingletonCompulsory();
    }
}
